from django.db import models

from apps.common.model import BaseModel
from django_pydantic_field import SchemaField

from apps.products.models import Category


class AreaImage(BaseModel):
    """
    应用领域图片
    """
    area = models.ForeignKey(
        'Area',
        on_delete=models.SET_NULL,
        null=True,
        related_name='images',
        verbose_name="所属应用领域"
    )
    file = models.ImageField(upload_to='area/images/', verbose_name="图片")
    name = models.CharField(max_length=255, blank=True, null=True, verbose_name="图片名称")

    def __str__(self):
        return f"{self.name}-{self.file}"

    class Meta:
        verbose_name = "应用领域图片"
        verbose_name_plural = "应用领域图片"
        ordering = ['created_at']


class Area(BaseModel):
    """
    应用领域
    """
    category = models.ForeignKey(
        Category,
        on_delete=models.SET_NULL,
        null=True,
        related_name='areas',
        verbose_name="所属类目"
    )
    title = models.CharField(max_length=255, verbose_name="标题")
    description = models.TextField(blank=True, null=True, verbose_name="详情描述")
    video_links = SchemaField(list[str], default=list, blank=True, null=True, verbose_name="视频列表")

    def __str__(self):
        return self.title

    class Meta:
        verbose_name = "应用领域"
        verbose_name_plural = "应用领域"

    @property
    def images(self):
        return self.images.all()
